package com.example.controller;

import com.example.entity.RestBean;
import com.example.entity.vote.VoteRecording;
import com.example.service.VoteService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/vote")
public class VoteController {

    @Resource
    VoteService service;

    @PostMapping("/count-list")
    public RestBean<List<Integer>> getVoteCount(int gid) {
        return RestBean.success(service.getVoteCountByGid(gid));
    }

    @GetMapping("/count-all-game")
    public RestBean<List<Map<String, Integer>>> getVoteCountAll() {
        return RestBean.success(service.getAllVoteCount());
    }

    @GetMapping("/count-all-singer")
    public RestBean<List<Map<String, Integer>>> getAllVoteCountSinger() {
        return RestBean.success(service.getAllVoteCountSinger());
    }

    @PostMapping("/count-uid")
    public RestBean<Integer> getVoteCountByUid(int uid) {
        return RestBean.success(service.getVoteCountByUid(uid));
    }

    @PostMapping("/is-vote")
    public RestBean<Integer> isVote(VoteRecording voteRecording) {
        Integer voteState = service.isVoteReCording(voteRecording);
        if (voteState != 3) {
            return RestBean.success(voteState);
        } else {
            return RestBean.failure(400, voteState);
        }
    }

    @PostMapping("/is-vote-sid")
    public RestBean<Integer> isVoteSid(VoteRecording voteRecording) {
        Integer voteState = service.isVoteReCording(voteRecording);
        if (voteState != 3) {
            return RestBean.success(voteState);
        } else {
            return RestBean.failure(400, voteState);
        }
    }

    @PostMapping("/add")
    public RestBean<String> add(VoteRecording voteRecording) {
        String s = service.addVoteReCording(voteRecording);
        if (s == null) {
            return RestBean.success("投票成功");
        } else {
            return RestBean.failure(400, s);
        }
    }

    @PostMapping("/delete")
    public RestBean<String> delete(int sid, int uid, int gid) {
        String s = service.deleteVoteRecordingByUidAndGid(sid, uid, gid);
        if (s == null) {
            return RestBean.success("取消投票成功");
        } else {
            return RestBean.failure(400, s);
        }
    }

}
